###############################################################################
 # Copyright (C) 2010 Chris Wroe
 #
 # Modifications to the initial code base are copyright of their respective
 # authors, or their employers as appropriate.
 #
 # This program is free software; you can redistribute it and/or modify it under
 # the terms of the GNU Lesser General Public License as published by the Free
 # Software Foundation; either version 2.1 of the License, or (at your option)
 # any later version.
 #
 # This program is distributed in the hope that it will be useful, but WITHOUT
 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 # details.
 #
 # You should have received a copy of the GNU Lesser General Public License
 # along with this program; if not, write to the Free Software Foundation, Inc.,
 # 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ##############################################################################
require "rubygems" 
require "neo4j"
require 'lucene'
require "model"

require "load_snomed_component"

include Lucene

class LoadSnomedDescriptionComponent < LoadSnomedComponent

  def initialize(format_file)
    @field_mapper = FieldToPropertyMap.new
    @field_mapper.load_config(format_file)
    
    @rtype = org.neo4j.graphdb.DynamicRelationshipType.withName(Description.class.to_s)
    @java_concept_rel_type = org.neo4j.graphdb.DynamicRelationshipType.withName('Concept#concept')
    super()
  end

  

  def create_component(line,inserter,global_properties)
    raise if !line or line.strip.empty?
    @field_mapper.data=line


    current_description_properties = {}.merge! global_properties
    current_description_properties.merge! @field_mapper.property_map

    current_description_properties['_classname'] =
      (current_description_properties['description_type']==1) ? 'Preferred_Description' : 'Description'
   

   

#    raise if tab_items[0].empty?
#    ext_id = tab_items.shift.strip.to_i
#    description_concept_node_id=current_description_properties['concept_node_id'] =
#      self.get_node_id_for_next_field(ext_id, @concept_index, {},:snomed_code)

    description_concept_node_id= @field_mapper.get_relationship_node('concept_id', global_properties)


    current_description_node_id=inserter.createNode(current_description_properties)
    
    inserter.createRelationship(current_description_node_id,description_concept_node_id,@java_concept_rel_type,nil)

    inserter.createRelationship(@index_node_id,current_description_node_id,@rtype,nil)

    @description_index << {:id => current_description_node_id.to_s ,
      :description_id => current_description_properties['description_id'],
      :term => current_description_properties['term'],
      :version => current_description_properties['version']
      }
  end

def batch_insert_from_file(f,global_properties)
    component_inserted_count =0


    @concept_index = Index.new('/Concept')
    @concept_index.field_infos[:name][:tokenized] = true
    @description_index = Index.new('/Description')
    @description_index.field_infos[:term][:tokenized] = true
    Neo4j::BatchInserter.new do |b|
       #skip header line
      read_row(f)

      while !f.eof?
        line = read_row(f)
        create_component(line,b,global_properties)
        component_inserted_count+=1
        if component_inserted_count % 75000 == 0
        puts component_inserted_count
        @description_index.commit
        puts "committed"
        end
      end
      @description_index.commit

    end
    return component_inserted_count
  end
 
end

